//go:build webview_window

package main

import (
	"fmt"
	"github.com/webview/webview"
	"log"
)

var shutdownQueue *chan bool

func init() {
	startupHandlers = append(startupHandlers, startDefaultWindow())
}

func startDefaultWindow(queue *chan bool) {
	fmt.Printf("Start default window.")
	shutdownQueue = queue
	w := webview.New(false)
	defer func() {
		w.Destroy()
		w.Terminate()
		fmt.Printf("Shutdown default window.")
		*shutdownQueue <- true
	}()
	w.SetTitle("MainWindow")
	w.SetSize(1700, 900, webview.HintNone)
	w.Bind("noop", func() string {
		log.Println("hello")
		return "hello"
	})
	w.Bind("add", func(a, b int) int {
		return a + b
	})
	w.Bind("quit", func() {
		w.Terminate()
	})
	w.Navigate("http://127.0.0.1:4000")
	w.SetHtml(`<!doctype html>
		<html>
			<body>hello</body>
			<script>
				window.onload = function() {
					document.body.innerText = ` + "`hello, ${navigator.userAgent}`" + `;
					noop().then(function(res) {
						console.log('noop res', res);
						add(1, 2).then(function(res) {
							console.log('add res', res);
							quit();
						});
					});
				};
			</script>
		</html>
	)`)
	w.Run()
}
